﻿using DevExpress.XtraEditors;
using System.Windows.Forms;
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using DevExpress.XtraEditors.Controls;
using Mota.EditModule;

namespace Mota.XMLControls
{
    /// <summary>
    /// Represent CheckedComboBox visual control based on DX CheckedComboBoxEdit control
    /// </summary>
    public class DXGuiCheckedComboBox:GuiCheckedComboBox
    {
        /// <summary>
        /// List for storing static values if given
        /// </summary>
        private List<string> staticItems = null;
        /// <summary>
        /// Create CheckedComboBoxEdit, add editvalue changed event handler
        /// Set BestFitMode to BestFitResizePopup
        /// Set TextEditStyle to DisableTextEditor mode
        /// </summary>
        public DXGuiCheckedComboBox()
        {
            visualControl = new MyDxCheckedComboBox();
            MyDxCheckedComboBox lue = visualControl as MyDxCheckedComboBox;
            lue.EditValueChanged += lue_EditValueChanged;
            lue.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
        }
        /// <summary>
        /// Method to add static string item to private  list with staticItems
        /// </summary>
        /// <param name="item">represent single static item</param>
        public override void AddStaticItem(string item)
        {
            if(staticItems == null)
            {
                // initialize list if not initialized before
                staticItems = new List<string>();
                (visualControl as MyDxCheckedComboBox).Properties.DataSource = staticItems;
            }
            staticItems.Add(item);
        }
        /// <summary>
        /// Event handler for EditValueChanged event
        /// Only for staticItems purpose
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">EventArgs</param>
        private void lue_EditValueChanged(object sender, EventArgs e)
        {
            var binding = (sender as MyDxCheckedComboBox).DataBindings["XmlItems"];
            if (binding != null)
                binding.WriteValue();
        }
        /// <summary>
        /// Getter return Text propertie of the LookUpEdit object
        /// Setter set Text propertie of the LookUpEdit object 
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as MyDxCheckedComboBox).Text;
            }
            set
            {
                (visualControl as MyDxCheckedComboBox).Text = value;
            }
        }
        /// <summary>
        /// Binding object for LookUpEdit
        /// getter return LookUpEdit.Properties.DataSource  value
        /// setter lookup value as BindingSource and try to parse values using _KeyField and _Fields attributes
        /// and add Range of columns
        /// </summary>
        public override object Binding
        {
            get
            {
                return (visualControl as MyDxCheckedComboBox).Properties.DataSource;
            }
            set
            {
                if (value is BindingSource)
                {
                    var vc = (visualControl as MyDxCheckedComboBox);
                    vc.Properties.BeginUpdate();
                    try
                    {

                        vc.Properties.DataSource = value;
                        vc.Properties.ValueMember = XmlAttributes["_KeyField"];
                        vc.Properties.DisplayMember = XmlAttributes["_Fields"].Split(',').Last();
                    }
                    finally
                    {
                        vc.Properties.EndUpdate();
                    }
                }
            }
        }

        /// <summary>
        /// Add DataBinding to LookUpEdit EditValue propertie
        /// </summary>
        /// <param name="valueBinding">main binding with current record values</param>
        public override void SetValue(object valueBinding)
        {
            if ((valueBinding is BindingSource) && ((visualControl as MyDxCheckedComboBox).DataBindings["XmlItems"]==null))
            {
                (visualControl as MyDxCheckedComboBox).DataBindings.Add("XmlItems", valueBinding as BindingSource, XmlAttributes["XMLAttribute"], true);
            }
        }

        public override void Refresh(ConnectionConfigs configs, BindingSource mainBs)
        {
            this.Rfsh(mainControls, mainBs, configs);
        }

        public override object GetValue()
        {
            return (visualControl as MyDxCheckedComboBox).XmlItems;
        }
    }
}
